package com.itdianzi.controller.task;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itdianzi.controller.base.BaseController;
import com.itdianzi.domain.cargo.Contract;
import com.itdianzi.domain.cargo.ContractProduct;
import com.itdianzi.domain.cargo.ContractProductExample;
import com.itdianzi.domain.cargo.Factory;
import com.itdianzi.export_common.common.utils.MailUtil;
import com.itdianzi.service.cargo.ContractProductService;
import com.itdianzi.service.cargo.ContractService;
import com.itdianzi.service.cargo.FactoryService;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MyTask extends BaseController {

    @Reference
    private ContractService contractService;

    @Reference
    private ContractProductService contractProductService;

    @Reference
    private FactoryService factoryService;

    public void run() throws Exception {
        System.out.println(new Date()+"qqqqqqq");
        Map<String, Date> map = new HashMap<>();
        List<Contract> all = contractService.findAll();
        System.out.println(all);
        for (Contract contract : all) {
            Date deliveryPeriod = contract.getDeliveryPeriod();
            long time = deliveryPeriod.getTime();
            long time1 = new Date().getTime();
            if(time-time1<=604800000&&time-time1>=0){
                System.out.println(contract.getId()+"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
                String id = contract.getId();
                ContractProductExample contractProductExample = new ContractProductExample();
                ContractProductExample.Criteria criteria = contractProductExample.createCriteria();
                criteria.andContractIdEqualTo(id);
                List<ContractProduct> findall = contractProductService.findAll(contractProductExample);
                for (ContractProduct contractProduct : findall) {
                    String factoryId = contractProduct.getFactoryId();
                    Factory factory = factoryService.findById(factoryId);
                    String factoryEmail = factory.getFactoryEmail();
                    System.out.println(factoryEmail+"ffffffffffffffffffffffffffffffff");
                    String contractTime = new SimpleDateFormat("yyyy-MM-dd").format(time);
                    sendEmail(factoryEmail,contractTime);
                }
            }
            map.put(contract.getId(),contract.getDeliveryPeriod());
        }

    }


    public void sendEmail(String email,String contractTime) throws Exception {
        String biaoti="交货提醒邮件";
        String shijian="交货日期为"+contractTime+"请注意时间安排";
        MailUtil.sendMsg(email,biaoti,shijian);
    }

}
